import {Component, Input, OnChanges, OnInit,} from '@angular/core';
import {BehalfService} from "../../../../service/behalf.service";
import {Behalf} from "../../../../interface/behalf";
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-behalf-children',
  templateUrl: './behalf-children.component.html',
  styleUrls: ['./behalf-children.component.scss']
})
export class BehalfChildrenComponent implements OnInit{

  constructor(private buhalfServer:BehalfService,private message: NzMessageService) {
  }
  ngOnInit(): void {
    this.getBehalfs()
  }
  ngAfterContentChecked(){
    if(this.type == 1){
        this.getBehalfs()
    }else if(this.type == 2){
        this.getBehalfs()
        this.behalfs = this.behalfs.filter((item)=>item.createName == this.user)
    }else if(this.type == 3){
        this.getBehalfs()
        this.behalfs = this.behalfs.filter((item)=>item.createName !== this.user)
    }
    // switch(this.type){
    //   case 1:
    //     this.getBehalfs()
    //     break;
    //   case 2:
    //     this.getBehalfs()
    //     this.behalfs = this.behalfs.filter((item)=>item.createName == this.user)
    //     break;
    //   case 3:
    //     this.getBehalfs()
    //     this.behalfs = this.behalfs.filter((item)=>item.createName !== this.user)
    //     break;
    // }
  }
  isVisible:boolean = false
  isSetData:boolean = false
  title:string = ''
  modalDetail:Behalf={id:0,title:'', createName:'', createTime:'', weight:0, isComplete:true, content:''}
  @Input() type:number=1
  @Input() user:string = '';
  behalfs:Behalf[] = []
  detailModal(data:Behalf): void {
    this.title = '查看代办'
    this.modalDetail = data
    this.controlShow()
    this.isSetData = true
  }
  addModal():void{
    this.clear()
    this.controlShow()
    this.title = '新增待办'
    this.isSetData = false
  }
  controlShow():void{
    this.isVisible = !this.isVisible;
  }
  clear():void{
    this.modalDetail.title = ''
    this.modalDetail.createName = ''
    this.modalDetail.createTime = ''
    this.modalDetail.weight = 0
    this.modalDetail.content = ''
  }
  commit():void{
    this.controlShow()
    this.modalDetail.id = Math.random()
    this.modalDetail.createName = this.user
    this.buhalfServer.add(this.modalDetail)
    this.getBehalfs()
    this.behalfs = this.behalfs.filter((item)=>item.createName == this.user)
    this.behalfs =  this.behalfs.sort((a,b)=> a.weight - b.weight)
  }
  delete(data:Behalf):void{
    if(data.createName == this.user){
      this.buhalfServer.delete(data.id)
      this.getBehalfs()
    }else{
      this.message.create('warning', `您没有权限删除${data.createName}的代办`);
    }
  }
    // 获取代办列表
    getBehalfs():void{
      this.buhalfServer.getBehalfs()
        .subscribe(heroes => this.behalfs = heroes);
    }
}
